package com.aaa.javaweb.dao.impl;

import com.aaa.javaweb.dao.SheShiDao;
import com.aaa.javaweb.entity.SheShi;
import com.aaa.javaweb.until.JDBCUntil;
import com.aaa.javaweb.until.TransBean;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
@Repository
public class SheShiDaoimpl implements SheShiDao {
    @Override
    //查询设施表的所有信息
    public List<Map> selectALL() {
        String sql = "select * from t_sheshi";
        return JDBCUntil.selectMap(sql);
    }

    @Override
    public int delone(String gid) {
        String sql = "delete from t_sheshi where id=?";
        return JDBCUntil.Updatekeys(sql,gid);
    }

    @Override
    public int addone(SheShi sheShi) {
        if (sheShi.getId()==null){
            String sql ="insert into t_sheshi(name,count,typeid,pmoney,gys) values(?,?,?,?,?)";
            return JDBCUntil.Updateout(sql,sheShi.getName(),sheShi.getCount(),sheShi.getTypeid(),sheShi.getPmoney(),sheShi.getGys());
        }else {
            String sql="update t_sheshi set name=?,count=?,typeid=?,pmoney=?,gys=? where id=?";
            return JDBCUntil.Updateout(sql,sheShi.getName(),sheShi.getCount(),sheShi.getTypeid(),sheShi.getPmoney(),sheShi.getGys(),sheShi.getId());
        }


    }

    @Override
    public List<SheShi> getBookIdAndNameByTypeId(int parseInt) {
        String sql = "select * from t_sheshi where typeid = ? ";
        List<Map> maps = JDBCUntil.selectMap(sql, parseInt);
        return TransBean.populate(SheShi.class, maps);
    }

    @Override
    public SheShi getBookCountAndPriceById(String ssid) {
        String sql = "select * from t_sheshi where id = ?";
        List<Map> maps = JDBCUntil.selectMap(sql, ssid);
        if (maps.size() > 0) {
            return (SheShi) TransBean.populate(SheShi.class, maps).get(0);
        }
        return null;
    }

    @Override
    public int selectXHcount() {
        String s = "select count(*) c from t_sheshi where typeid=1 and count>0 ";
        List<Map> maps = JDBCUntil.selectMap(s);
        if (maps.size() > 0) {
            return Integer.parseInt(String.valueOf(maps.get(0).get("c")));
        }
        return 0;
    }


}
